﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace Pavilion.DesignerTool
{
    public class AnimationContainerViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        
        public NotifiableCollection<ResourceData> AnimationResourceCollection { get; private set; }
        public NotifiableCollection<AnimationLayer> AnimationLayerCollection { get; private set; }
        public AnimationLayer CurrentAnimationLayer { get; set; }

        public AnimationSelectionInfo AnimationSelectionInfo { get; private set; }
        public UndoRedoController UndeRedoController { get { return IsCurrentAnimationDataNull ? null : CurrentAnimationLayer.CurrentAnimationData.UndoRedoController; } }

        private bool IsCurrentAnimationDataNull { get { return CurrentAnimationLayer == null || CurrentAnimationLayer.CurrentAnimationData == null; } }
        private IEnumerable<Frame> CopiedFrames;

        public AnimationContainerViewModel()
        {
            AnimationSelectionInfo = new AnimationSelectionInfo();
            AnimationLayerCollection = new NotifiableCollection<AnimationLayer>();
        }

        public void LoadResources(NotifiableCollection<ResourceData> animationResourceCollection)
        {
            AnimationResourceCollection = animationResourceCollection;
        }

        public void UpdateSelectionInfo()
        {
            IEnumerable<AnimationLayer> backgroundAnimationLayers = AnimationLayerCollection.Where((x) => x != CurrentAnimationLayer);
            AnimationSelectionInfo.UpdateSelectionInfo(CurrentAnimationLayer, backgroundAnimationLayers);
            OnPropertyChanged("UndeRedoController");
        }

        public void CreateAnimationLayer()
        {
            CurrentAnimationLayer = new AnimationLayer(AnimationResourceCollection);
            AnimationLayerCollection.Add(CurrentAnimationLayer);

            OnPropertyChanged("CurrentAnimationLayer");
        }

        public void RemoveAnimationLayer(AnimationLayer animationLayer)
        {
            AnimationLayerCollection.Remove(animationLayer);
        }

        public void CreateFrame()
        {
            CurrentAnimationLayer.AddNewFrame();
        }

        public void RemoveFrames(IEnumerable<FrameData> framesToRemove)
        {
            foreach (FrameData frameData in framesToRemove)
                CurrentAnimationLayer.RemoveFrame(frameData);
        }

        public void CopySelectedFrames(IEnumerable<FrameData> framesToCopy)
        {
            CopiedFrames = framesToCopy.Select(x => x.Frame.Clone()).ToArray();
        }

        public void PasteFrames()
        {
            if (CopiedFrames == null)
                return;

            CurrentAnimationLayer.PasteFrames(CopiedFrames.Select(x => x.Clone()));
        }

        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
